##########################################################################################################################################################
### Setup

### Source setup
source("code/1_modeling_setup.R")



#########################################################################################################################################################
### Run the restricted threshold analysis

### Thresholds between 3/5 and 2/3

# Make dataframe
thresh <- filter(votes, (yea_prop > (3/5)) & (yea_prop < (2/3)) & (veto_override_threshold!=0.5) ) %>%
  mutate(low_threshold = ifelse(veto_override_threshold==0.6, '3/5', '2/3') %>% as.factor() %>% relevel(ref='3/5')) %>%
  dplyr::select(c(bill_id, law_sles, low_threshold, divided3, maj_sponsor, num_cs, maj_share, pty_diff, mds1, chamber, committee_sponsor, maj_unity, gov_pty_override, lineitemveto, year, state)) %>% 
  distinct()

# Estimate
mods_thresh <- glm(law_sles ~ low_threshold * divided3 + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + chamber + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), data=filter(thresh, !state %in% high_missingness), family='binomial')

# Substantive effect size
law_sd <- thresh %>% 
  filter(!state %in% high_missingness) %>% 
  pull(law_sles) %>% 
  sd()
sub_eff <- plot_model(mods_thresh, type='pred', terms=c('low_threshold', 'divided3'))$data %>%
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_x = predicted[x==1],
    x_text = min(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/law_sd))

# Marginal effects plot
thresh_plot <- plot_model(mods_thresh, type='pred', terms=c('low_threshold', 'divided3'), colors='bw', line.size=2, dot.size=6) +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  theme_bw() + 
  xlab('Veto Override Threshold') +
  ylab('Probability of Becoming Law') +
  ggtitle('Bills Passing with 3/5 to 2/3 Yeas') +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(shape='') +
  geom_text(data=sub_eff, aes(x=x_text, y=pred_x, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=-0.2, vjust=-0.4, position=position_dodge(width=0.1), size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")


### Thresholds between 1/2 and 3/5

# Make dataframe
thresh2 <- filter(votes, (yea_prop > (1/2)) & (yea_prop < (3/5)) & (veto_override_threshold!=0.67) ) %>%
  mutate(low_threshold = ifelse(veto_override_threshold==0.5, '1/2', '3/5') %>% as.factor() %>% relevel(ref='1/2')) %>%
  dplyr::select(c(bill_id, law_sles, low_threshold, divided3, maj_sponsor, num_cs, maj_share, pty_diff, mds1, chamber, committee_sponsor, maj_unity, gov_pty_override, lineitemveto, year, state)) %>% 
  distinct()

# Estimate
mods_thresh2 <- glm(law_sles ~ low_threshold * divided3 + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + chamber + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), data=filter(thresh2, !state %in% high_missingness), family='binomial')

# Substantive effect size
law_sd2 <- thresh2 %>% 
  filter(!state %in% high_missingness) %>% 
  pull(law_sles) %>% 
  sd()
sub_eff2 <- plot_model(mods_thresh2, type='pred', terms=c('low_threshold', 'divided3'))$data %>%
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_x = predicted[x==2],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/law_sd2))

# Marginal effects plot
thresh_plot2 <- plot_model(mods_thresh2, type='pred', terms=c('low_threshold', 'divided3'), colors='bw', line.size=2, dot.size=6) +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  theme_bw() + 
  xlab('Veto Override Threshold') +
  ylab('Probability of Becoming Law') +
  ggtitle('Bills Passing with 1/2 to 3/5 Yeas') +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(shape='') +
  geom_text(data=sub_eff2, aes(x=x_text, y=pred_x, label=paste0(round(eff_size_dv, 2), ' SDs'), 
                               group=eff_size_dv), inherit.aes = FALSE, hjust=1.4, vjust=0.8, size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")


### Thresholds between 1/2 and 3/5

# Make dataframe
thresh3 <- filter(votes, (yea_prop > (1/2)) & (yea_prop < (2/3)) & (veto_override_threshold!=0.6) ) %>%
  mutate(low_threshold = ifelse(veto_override_threshold==0.5, '1/2', '2/3') %>% as.factor() %>% relevel(ref='1/2')) %>%
  dplyr::select(c(bill_id, law_sles, low_threshold, divided3, maj_sponsor, num_cs, maj_share, pty_diff, mds1, chamber, committee_sponsor, maj_unity, gov_pty_override, lineitemveto, year, state)) %>% 
  distinct()

# Estimate
mods_thresh3 <- glm(law_sles ~ low_threshold * divided3 + maj_sponsor + num_cs + maj_share + pty_diff + mds1 + chamber + committee_sponsor + maj_unity + gov_pty_override + as.factor(year), data=filter(thresh3, !state %in% high_missingness), family='binomial')

# Substantive effect size
law_sd3 <- thresh3 %>% 
  filter(!state %in% high_missingness) %>% 
  pull(law_sles) %>% 
  sd()
sub_eff3 <- plot_model(mods_thresh3, type='pred', terms=c('low_threshold', 'divided3'))$data %>%
  as.data.frame() %>%
  dplyr::select(x, predicted, group) %>%
  group_by(group) %>%
  summarise(
    eff_size = diff(predicted),
    pred_x = predicted[x==2],
    x_text = max(x)
  ) %>%
  mutate(eff_size_dv=abs(eff_size/law_sd3)) 

# Marginal effects plot
thresh_plot3 <- plot_model(mods_thresh3, type='pred', terms=c('low_threshold', 'divided3'), colors='bw', line.size=2, dot.size=6) +
  geom_line(linetype='dotted', linewidth=1.2, position=position_dodge(width=0.1)) +
  theme_bw() + 
  xlab('Veto Override Threshold') +
  ylab('Probability of Becoming Law') +
  ggtitle('Bills Passing with 1/2 to 2/3 Yeas') +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(shape='') +
  geom_text(data=sub_eff3, aes(x=x_text, y=pred_x, label=paste0(round(eff_size_dv, 2), ' SDs'), group=eff_size_dv), 
            inherit.aes = FALSE, hjust=1.2, position=position_dodge(width=0.1), size=6)  +
  theme(text=element_text(size=16),
        axis.text=element_text(size=16),
        legend.position="none")


### Save together

# Get the legend
plot_legend <- plot_model(mods_thresh, type='pred', terms=c('low_threshold', 'divided3'), colors='bw', line.size=2, dot.size=5) + 
  labs(shape='') +
  theme_bw() +
  theme(legend.text=element_text(size=20), legend.title=element_text(size=20)) +
  guides(shape = guide_legend(override.aes = list(size=12))) 
leg_plot <- cowplot::get_legend(plot_legend) %>%
  as_ggplot() + 
  theme(legend.key.size=unit(10,'cm'))

# Combine plots
thresh_combine <- thresh_plot + thresh_plot2 + thresh_plot3 + leg_plot + plot_layout(ncol=2)

# Save
ggsave("results/fig4_restricted_threshold.pdf", plot=thresh_combine, width=10, height=10)
